/**
 * 
 */
package com.wgo.precise.client.ui.view.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;

import com.wgo.precise.client.ui.controller.Assert;


/**
 * @author petterei
 *
 */
public class ReflectionRunnableWithProgress implements IRunnableWithProgress {

	private Method method;
	private Object target;
	private String name;
	private Object[] args;
	
	public ReflectionRunnableWithProgress(String name, Method method, Object target, Object... args) {
		Assert.notNull(method);
		Assert.notNull(target);
		this.method = method;
		this.target = target;
		this.name = name;
		this.args = args;
	}

	public ReflectionRunnableWithProgress(String name, String methodName, Class parameterTypes[], Object target, Object... args) {
		this(name, ReflectionHelper.retrieveMethod(target.getClass().getSimpleName(), target.getClass(), methodName), target, args);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
	 */
	public void run(IProgressMonitor monitor) throws InvocationTargetException,
			InterruptedException {
		try {
			monitor.beginTask(name, IProgressMonitor.UNKNOWN);
			ReflectionHelper.invoke("Error invoking operation: " + name, target, method, args);
		} finally {
			monitor.done();
		}

	}

}
